This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

sd_ble_gap_address_set works for normal mode only?

Hi,

I have learned that you can change the MAC address on a device by using sd_ble_gap_address_set. However, when I press the sw2 button on the beacon (running a modified version of the ble_app_beacon app), the beacon switches back to its original MAC address. I don't understand why this is happening. How can I set the MAC address so that it will stay the same for both normal and config modes?

Thanks!

  • Yes, I realized that I was calling the function even before flash init. Moving it down the main function helped get rid of it. I also observe another similar problem. I programmatically switch between config and and normal modes in my app and try to update the MAC address after there is a change in one of the characteristics in one of the services. For that I try to update the MAC address in beacon_write_handler function. However, I do not observe this change. What am I doing wrong? The following is how I updated the beacon_write_handler function.

    			case beacon_mac_data:
    			memcpy(&tmp.data.mac_address, data, BCS_DATA_MAC_LEN); 
    			//Also can change the beacons MAC address
    			change_mac_address(tmp.data.mac_address);
    			break;
    

    //Manually change the MAC address to the desired value void change_mac_address(uint8_t my_random_addr[BLE_GAP_ADDR_LEN]){ ble_gap_addr_t gap_addr; uint8_t err_code;

    	// "my_random_addr" must be random generated.
    	//const uint8_t my_random_addr[BLE_GAP_ADDR_LEN] = {6,5,4,3,2,1}; // random address
    	
    	gap_addr.addr_type = BLE_GAP_ADDR_TYPE_RANDOM_STATIC;
    	memcpy(&gap_addr.addr, my_random_addr, sizeof(gap_addr.addr));
    	gap_addr.addr[5] |= 0xc0; // 2 MSBit must be '11' for RANDOM_STATIC address, see v4.0, Vol 3, Part C, chapter 10.8
    	err_code = sd_ble_gap_address_set(BLE_GAP_ADDR_CYCLE_MODE_NONE, &gap_addr);
    	APP_ERROR_CHECK(err_code);
    

    }

  • I'm not sure what you are doing wrong. Have you tried to use the debugger? Place a breakpoint inside the function to see that it is actually called? Does sd_ble_gap_address_set() return with NRF_SUCCESS? Place a breakpoint on APP_ERROR_CHECK. To see the actual error you need to turn off optimizations, In Keil: Options for Target->C/C++->Optimizations->Level 0

  • sd_ble_gap_address_set returns with 0, which is the value of NRF_SUCCESS so I think this is fine. I believe it has something to do with beacon getting reset after a disconnect, but I'm not sure (Even before disconnect, the address doesnt change, but I thought that could be because mac address doesnt change while connected to a BLE device).

  • I figured out how to do this. The main function is called after evert reset, so the value in the MAC characteristic needs to be read from flash and then this value should be used to set the device MAC address. It works after that.

Related